﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    public class AnnnualVacation
    {
        /// <summary>
        /// 获得年假天数
        /// </summary>
        public static Decimal getTotalCntOfAnnnualVac(string UserId)
        {
            StringBuilder strSql = new StringBuilder();
            Decimal cntOfAnnnualVac = 5;
            strSql.Append("select dbo.FN_GetTotalCntOfAnnnualVac(UserId) CntOfTotal ");
            strSql.Append(" FROM Hr_UserInfo ");
            strSql.Append(" where UserId='" + UserId + "'");

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                if (dataReader.Read())
                {
                    cntOfAnnnualVac = (Decimal)dataReader["CntOfTotal"];
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return cntOfAnnnualVac;
        }

        /// <summary>
        /// 获得已用年假天数
        /// </summary>
        public static Decimal getUsedCntOfAnnnualVac(string UserId, string RecordId)
        {
            StringBuilder strSql = new StringBuilder();
            Decimal cntOfAnnnualVac = 0;
            strSql.Append("SELECT SUM(Days) AS CntOfUsed ");
            strSql.Append(" FROM Hr_VacationRecord ");
            strSql.Append(" WHERE (VacationType = '年休假')");
            strSql.Append(" AND (DATEPART(year, DateTime) = DATEPART(year,GETDATE()))");
            strSql.Append(" AND (UserId = '" + UserId + "')");

            if (RecordId != "")
            {
                strSql.Append(" AND ((RecordId > '" + RecordId + "')");
                strSql.Append(" OR (RecordId < '" + RecordId + "'))");
            }

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                if (dataReader.Read())
                {
                    if (dataReader["CntOfUsed"] != DBNull.Value)
                    {
                        cntOfAnnnualVac = (Decimal)dataReader["CntOfUsed"];
                    }
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return cntOfAnnnualVac;
        }

        /// <summary>
        /// 年假使用情况
        /// </summary>
        public static DataSet getListOfAnnnualVac(string TableNm,string strSelect, string TableFieldList, string strWhere, string strOrder, int intCurPage, int intRowCnt, Boolean bDoCount)
        {

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, intCurPage);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, intRowCnt);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, strOrder);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableNm);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, strSelect);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, bDoCount);
            return db.ExecuteDataSet(dbCommand);
        }
    }
}
